package xj.graph2d.app.project;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import xj.graph2d.Document;
import xj.graph2d.DocumentException;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

/**
 *  Rename a project item or folder 
 */
public class ProjectItemPropertiesListener 
  extends DrawAction 
  implements ActionListener {

  class ProjectItemOptionPanel extends DialogPanel {

    public ProjectItemOptionPanel(String name) {
      super(name);
      nameLabel = new JLabel(getResourceString("ProjectItemName.text") + " ");
      nameField = new JTextField();

      JLabel typeLabel = new JLabel(getResourceString("ProjectItemType.text") + " ");
      typeField = new JComboBox();
      typeField.addItem("UML Class Diagram");
      typeField.addItem("UML Package Diagram");
      typeField.addItem("UML Use Case Diagram");
      typeField.addItem("UML State Diagram");
      typeField.addItem("UML Sequence Diagram");
      typeField.addItem("UML Activity Diagram");
      typeField.setEnabled(false);

      JLabel authorLabel = new JLabel(getResourceString("Author.text") + " ");
      authorField = new JTextField();
      authorField.setEnabled(false);
      JLabel titleLabel = new JLabel(getResourceString("Title.text") + " ");
      titleField = new JTextField();
      titleField.setEnabled(false);
      JLabel orgLabel = new JLabel(getResourceString("Organization.text") + " ");
      orgField = new JTextField();
      orgField.setEnabled(false);

      JLabel descriptionLabel = 
	new JLabel(getResourceString("Description.text") + " ");
      descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
      descriptionField = new JTextArea(4, 40);
      descriptionField.setBorder(BorderFactory.createEtchedBorder());
      descriptionField.setEnabled(false);

      JLabel ctimeLabel1 = new JLabel(getResourceString("Create.text") + " ");
      ctimeLabel = new JLabel();
      JLabel mtimeLabel1 = new JLabel(getResourceString("LastModified.text") + " ");
      mtimeLabel = new JLabel();

      addRow(nameLabel, nameField);
      addRow(typeLabel, typeField);
      addRow(titleLabel, titleField);
      addRow(authorLabel, authorField);
      addRow(orgLabel, orgField);
      addRow(descriptionLabel, new JScrollPane(descriptionField));
      addRow(ctimeLabel1, ctimeLabel);
      addRow(mtimeLabel1, mtimeLabel);

      setPreferredSize(new Dimension(400, 370));
      setOption();
    }

    @Override
    public void setOption() {
      if (item != null) {
	SimpleDrawDocument doc = item.getDocument();
	String name = doc.getName();
	Document.DocumentType type = doc.getDocumentType();
	String typeStr = type.toString();
	typeStr = typeStr.replaceAll("_", " ");
	String title = doc.getTitle();
	String author = doc.getAuthor();
	String org = doc.getOrganization();
	String des = doc.getDescription();
	  
	nameField.setText(name);
	typeField.setSelectedItem(typeStr);
	authorField.setText(author);
	titleField.setText(title);
	orgField.setText(org);
	descriptionField.setText(des);
	ctimeLabel.setText((new Date(doc.getCreationTime())).toString());
	mtimeLabel.setText((new Date(doc.getLastModificationTime())).toString());
      }
    }

    @Override
    public void applyOption() {
      if (item != null) {
	boolean exists = false; 
	String name = nameField.getText();

	if (name != null && 
	    !name.equals(item.getName())) {
	  ProjectFolder parent = item.getParent();
	  if (parent != null) { 
	    String path = parent.getFullPath();
	    String filename = path + File.separator + name;
	    if (!(item instanceof ProjectFolder)) {
	      String ext = "." + main.getDocumentExtension(item.getItemType());
	      filename += ext;
	    }
	    File file = new File(filename);
	    exists = file.exists();
	  }
	  
	  if (exists) { 
	    String title, message;
	    title = getResourceString("MoveItem.Error.title");
	    message = getResourceString("MoveItem.Error.message");
	    message = MessageFormat.format(message, name, parent.getName());
	    JOptionPane.showMessageDialog(main.getJFrame(), 
					  message, title, 
					  JOptionPane.ERROR_MESSAGE);
	  } else { 
	    String oldfilename = item.getDocumentFullPath();
	    File oldfile = new File(oldfilename);
	    oldfile.delete();

	    item.setName(name);
	    String filename = item.getDocumentFullPath();
	    Project project = item.getOwner();
	    String loc = project.getWorkingDir();
	    filename = Project.getRelativePath(loc, filename);
	    SimpleDrawDocument doc = item.getDocument();
	    doc.setFilename(filename);
	    main.updateDocumentTitle(doc);
	    
	    ProjectListener listener = main.getProjectListener();
	    listener.projectItemRenamed(item);

	    try { 
	      project.saveProject();
	    } catch (IOException e) {
	      System.err.println(e); 
	    } catch (DocumentException e) { 
	      System.err.println(e);
	    }
	  }
	}
      }
    }

    protected JTextField nameField;
    protected JLabel     nameLabel;

    protected JComboBox typeField;

    protected JTextField authorField;

    protected JTextField titleField;

    protected JTextField orgField;

    protected JTextArea descriptionField;

    protected JLabel ctimeLabel;

    protected JLabel mtimeLabel;
  }

  protected TitledDialogPanel projectItemOptionDialog = null;
  protected ProjectItemOptionPanel projectItemPanel = null;

  protected String projectItemDialogTitle;
  protected String projectItemHeader;
  protected String[] projectItemText;
  protected String projectItemName;
  protected Icon projectItemIcon;

  protected ProjectItem item;

  @Override
  protected void init() { 
    projectItemDialogTitle = getResourceString("ProjectItemDialog.title");
    projectItemName = getResourceString("ProjectItemName.text");
    projectItemHeader = getResourceString("ProjectItemDialog.header");
    projectItemText = new String[] { 
	getResourceString("ProjectItemDialog.text1"),
	getResourceString("ProjectItemDialog.text2"),	
    };
    projectItemIcon = DrawApp.getIcon("NewWizIcon");
  }

  public void setProjectItem(ProjectItem item) {
    this.item = item;
  }

  public void actionPerformed(ActionEvent e) {
    if (projectItemOptionDialog == null) {
      DialogPanel[] options = { 
	projectItemPanel = new ProjectItemOptionPanel(null), //projectItemPanelTitle),
      };

      projectItemOptionDialog = new TitledDialogPanel(main.getJFrame(),  
						      JOptionPane.OK_CANCEL_OPTION, 
						      null, options);
    }

    projectItemPanel.nameLabel.setText(projectItemName);
    projectItemOptionDialog.setTitle(projectItemDialogTitle);
    projectItemOptionDialog.setTitlePaneText(projectItemHeader, projectItemText,
					     projectItemIcon);

    projectItemOptionDialog.setOptions();
    projectItemOptionDialog.showOptionDialog();
  }

}
